//util.c
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <windows.h>


wchar_t* CharToWchar(const char* c)
{
    wchar_t *m_wchar;
    int len = MultiByteToWideChar(CP_ACP, 0, c, strlen(c), NULL, 0);
    m_wchar = new wchar_t[len + 1];
    MultiByteToWideChar(CP_ACP, 0, c, strlen(c), m_wchar, len);
    m_wchar[len] = '\0';
    return m_wchar;
}

char* WcharToChar(const wchar_t* wp)
{
    char *m_char;
    int len = WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
    m_char = new char[len + 1];
    WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), m_char, len, NULL, NULL);
    m_char[len] = '\0';
    return m_char;
}

/////////////////////////////////// util function /////////////////////////////////////

// char2由两个char组成 把两个ASCII码转换为一个BYTE
BYTE HexCharToByte(char *char2)
{
    BYTE value = 0;
    BYTE i = 0;

    for(i = 0; i < 2; i++)
    {
        value *= 16;
        if(('0' <= *char2) && (*char2 <= '9'))
        {
            value += *char2 - '0';
        }
        else if(('a' <= *char2) && (*char2 <= 'f'))
        {
            value += *char2 - 'a' + 10;
        }
        else if(('A' <= *char2) && (*char2 <= 'F'))
        {
            value += *char2 - 'A' + 10;
        }

        char2++;
		if(*char2 == '\0')
		{
			break;
		}

    }
    return value;
}
char * ByteToHexChar(BYTE value, char *char2)
{
    BYTE value2 = value >> 4;
    if(value2 <= 9)
    {
        char2[0] = '0' + value2;
    }
    else
    {
        char2[0] = 'A' + (value2 - 10);
    }

    value2 = value & 0x0F;
    if(value2 <= 9)
    {
        char2[1] = '0' + value2;
    }
    else
    {
        char2[1] = 'A' + (value2 - 10);
    }
    return char2;

}


char *nextHexChar(char * next, BYTE *value)
{
	while(!isxdigit(*next))
	{
		next++;
		if(*next == '\0') { return NULL; }
	}
	*value = HexCharToByte(next);
	next += 2;
	return next;
	
}

char * hexStringToCharString(char *value, int *ret_len)
{
	int len = strlen(value);
	BYTE *buf = (BYTE*) malloc(len);
	char *next = value;
	int index = 0;
	while((next = nextHexChar(next, &buf[index])) != NULL)
	{
		index++;
	}
	buf[index] = '\0';
	*ret_len = index;
	return (char*)buf;
}

char * charStringToHexString(char *value, int len)
{
	char *display_buf = (char *) malloc(len*3+1);
	int i;
	for(i=0; i<len; i++)
	{
		ByteToHexChar(value[i], &display_buf[i*3]);
		display_buf[i*3+2] = ' ';
	}
	display_buf[len*3] = '\0';
	return display_buf;
}

char * log(char * strValue)
{
	time_t time_log = time(NULL);
    struct tm* tm_log = localtime(&time_log);
	
	struct timeval time_now = {0};
	//long time_sec = 0;//秒
	long time_mil = 0;//1毫秒 = 1秒/1000 
	//long time_mic = 0;//1微秒 = 1毫秒/1000
	
	char *buf = (char*)malloc(strlen(strValue)+30);

	gettimeofday(&time_now,NULL);
	//time_sec = time_now.tv_sec;
	//time_mil = time_sec * 1000 + time_now.tv_usec/1000;
	//time_mic = time_now.tv_sec*1000*1000 + time_now.tv_usec;
	time_mil = time_now.tv_usec/1000;

    sprintf(buf, "\n[%04d-%02d-%02d %02d:%02d:%02d %03d] %s", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec, 
		(int)time_mil, strValue);
		
	return buf;


}